package com.zy.test.widget.fragment

import android.content.res.ColorStateList
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.google.android.material.navigation.NavigationBarView
import com.zy.test.widget.R


//参考 : https://blog.csdn.net/LoveFHM/article/details/125498372
class BottomNavigationViewFragment : Fragment() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_test_bottom_navigation_view, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView(view)
    }

    private lateinit var fragments: Array<Fragment>


    //监听Item
    private val mOnItemSelectedListener: NavigationBarView.OnItemSelectedListener =
        NavigationBarView.OnItemSelectedListener { item ->
            when (item.itemId) {
                R.id.navigation_android -> {
                    replaceFragment(fragments[0], fragments[0].tag.toString())
                    return@OnItemSelectedListener true
                }
                R.id.navigation_up -> {
                    replaceFragment(fragments[1], fragments[1].tag.toString())
                    return@OnItemSelectedListener true
                }
                R.id.navigation_find -> {
                    replaceFragment(fragments[2], fragments[2].tag.toString())
                    return@OnItemSelectedListener true
                }
                R.id.navigation_settings -> {
                    replaceFragment(fragments[3], fragments[3].tag.toString())
                    return@OnItemSelectedListener true
                }
            }
            false
        }


    private fun initView(view: View) {
        val navigation: BottomNavigationView = view.findViewById(R.id.nav_menu)
        navigation.setOnItemSelectedListener(mOnItemSelectedListener)
        //navigation.itemIconTintList = null
        navigation.itemTextColor =
            ColorStateList.valueOf(
                ContextCompat.getColor(
                    requireContext(),
                    com.zy.resource.R.color.black
                )
            )

        fragments = arrayOf(
            TabLayoutFragment.newInstance("Android"),
            TabLayoutFragment.newInstance("算法"),
            TabLayoutFragment.newInstance("发现"),
            TabLayoutFragment.newInstance("设置")
        )
        fragments.forEach {
            addFragment(it, it.tag.toString())
            hideFragment(it)
        }
        showFragment(fragments[0])
    }

    //添加Fragment到FragmentList中
    private fun addFragment(fragment: Fragment, tag: String) {
        val fragmentManager = parentFragmentManager
        val transaction: FragmentTransaction = fragmentManager.beginTransaction()
        transaction.add(R.id.fragment_container, fragment, tag)
        transaction.commit()
    }

    // 清空fragmentList的所有Fragment，替换成新的Fragment，注意Fragment里面的坑
    private fun replaceFragment(fragment: Fragment, tag: String) {
        val fragmentManager = parentFragmentManager
        val transaction: FragmentTransaction = fragmentManager.beginTransaction()
        transaction.replace(R.id.fragment_container, fragment, tag)
        transaction.commit()
    }

    //把Fragment设置成显示状态，但是并没有添加到FragmentList中
    private fun showFragment(fragment: Fragment) {
        val fragmentManager = parentFragmentManager
        val transaction: FragmentTransaction = fragmentManager.beginTransaction()
        transaction.show(fragment)
        transaction.commit()
    }

    //把Fragment设置成显示状态，但是并没有添加到FragmentList中
    private fun hideFragment(fragment: Fragment) {
        val fragmentManager = parentFragmentManager
        val transaction: FragmentTransaction = fragmentManager.beginTransaction()
        transaction.hide(fragment)
        transaction.commit()
    }


    companion object {

        @JvmStatic
        fun newInstance() =
            BottomNavigationViewFragment().apply {
                arguments = Bundle().apply {
                    putString("ARG_PARAM1", "param1")
                    putString("ARG_PARAM2", "param2")
                }
            }
    }
}